<template>
  <LDrawerForm
    :visible="visible"
    :confirmLoading="confirmLoading"
    :title="title"
    ref="onlyofficeAddForm"
    :formAttributes="formAttributes"
    @handleOk="handleOk"
    @handleCancel="handleCancel"
    @changeEventHandler="changeEventHandler">
  </LDrawerForm>
</template>

<script>
import LDrawerForm from '@/components/lt/form/LDrawerForm'
import pick from 'lodash.pick'
import request from '@/utils/request'
import defaultSettings from '@/config/defaultSettings'

export default {
    name: 'onlyofficeAddForm',
    components: {
        LDrawerForm
    },
    mixins: [],
    data () {
        return {
            title: '详情',
            visible: false,
            confirmLoading: false,
            model: {},
            defaultSettings,
            formAttributes: {
              type: '', // collapse,tabs
              formItems: [
                {
                  title: '标签一',
                  columns: 1,
                  fields: [
                    { field: 'fileName', label: '文件名称', component: 'input',rules: [{required: true, message: '不能为空'}]},
                    { field: 'fileType', label: '文件类型', component: 'select',dictCode: defaultSettings.数据字典.文档类型, initialValue: 'docx',rules: [{required: true, message: '不能为空'}] }
                  ]
                }
              ]

            }

        }
    },
    methods: {
        add () {
            this.edit({})
        },
        edit (record) {
            this.visible = true
            this.model = Object.assign({}, record)
            this.$nextTick(() => {
                this.$refs.onlyofficeAddForm.getForm().setFieldsValue(pick(this.model, 'fileName', 'fileType'))
            })
        },
        handleOk () {
            const that = this
            this.$refs.onlyofficeAddForm.getForm().validateFields((err, values) => {
                if (!err) {
                    that.confirmLoading = true
                    const formData = Object.assign(this.model, values)
                    request({ url: '/onlyofficeController/save', method: 'post', data: formData }).then((res) => {
                        if (res.success) {
                            that.close()
                            that.$message.success(res.message)
                        } else {
                            that.$message.warning(res.message)
                        }
                    }).finally(() => {
                        that.confirmLoading = false
                     //   that.close()
                    })
                }
            })
        },
        handleCancel () {
            this.visible = false
        },
        close () {
            this.$emit('close')
            this.visible = false
        },
         /**
         * 表单内容发生变化
         */
        changeEventHandler (changeItem) {

        },
        clickEventHandler (field, form) {

        }

    }
}
</script>
